// react + ts
// useState初始值为null

import { useState } from "react"

type User = {
  name: string
  age: number
}

function App03() {
  
  const [user, setUser] = useState<User | null>(null)

  const [user2, setUser2] = useState<User>(() => {
    return {
      name: 'Jack',
      age: 18
    }
  })

  // 
  const changeUser = () => {
    setUser(null)
    setUser({
      name: 'Jack',
      age: 18
    })
  }
  
  // 为了类型安全，可选链做类型守卫
  // 只要user不为null（不为空值）的时候才进行点运算
  return (
    <>
      this is app {user?.name}
    </>
  )
}

export default App03
